import {Injectable} from '@angular/core';
import {Http} from '@angular/http';
import {Observable} from 'rxjs/Observable';
import 'rxjs/Rx';
import {CacheService} from "ionic-cache/ionic-cache";
import { AppGlobal } from '../app/app.global';

let ADMIN_API_HOST = AppGlobal.ADMIN_HOST + '/api/v1';
let products_URL = AppGlobal.API_HOST + '/products';
let order_URL = AppGlobal.API_HOST + '/orders';
let categories_URL = AppGlobal.API_HOST + '/categories';
let post_URL = AppGlobal.API_HOST + '/posts';

@Injectable()
export class DataService {

    private http:Http;
    private cache:CacheService;

    constructor (http:Http, cache: CacheService) {
        this.http = http;
        this.cache = cache;
    }

    getProducts() {
        let url = products_URL;
        let cacheKey = url;
        let request = this.http.get(url)
            .map(res => res.json())
            .catch(this.handleError);
        return this.cache.loadFromObservable(cacheKey, request, 'groupKey', 1 * 60);
    }

    postOrder(data) {
        return this.http.post(order_URL, data)
            .map(res => res.json())
            .catch(this.handleError);
    }

    getCategories() {
        let url = categories_URL;
        let cacheKey = url;
        let request = this.http.get(url)
            .map(res => res.json())
            .catch(this.handleError);
        return this.cache.loadFromObservable(cacheKey, request, 'groupKey', 1 * 60);
    }

    getPosts(category_id) {
        let url = post_URL + "?1=1&category_id=" + category_id;
        let cacheKey = url;
        let request = this.http.get(url)
            .map(res => res.json())
            .catch(this.handleError);
        return this.cache.loadFromObservable(cacheKey, request, 'groupKey', 1 * 60);
    }

    getAppointments(customer_id, status) {
        let url = ADMIN_API_HOST + '/appointments/?1=1&customer_id=' + customer_id + '&status=' + status;
        let cacheKey = url;
        let request = this.http.get(url)
            .map(res => res.json())
            .catch(this.handleError);
        return this.cache.loadFromObservable(cacheKey, request);
    }

    getSchedules(begintime, endtime) {
        let url = ADMIN_API_HOST + '/timetable/list?1=1&begintime=' + begintime + '&endtime=' + endtime;
        let cacheKey = url;
        let request = this.http.get(url)
            .map(res => res.json())
            .catch(this.handleError);
        return this.cache.loadFromObservable(cacheKey, request);
    }

    getClass(timetable_id, customer_id) {
        let url = ADMIN_API_HOST + '/timetable?1=1&customer_id=' + customer_id + '&timetable_id=' + timetable_id;
        let cacheKey = url;
        let request = this.http.get(url)
            .map(res => res.json())
            .catch(this.handleError);
        return this.cache.loadFromObservable(cacheKey, request);
    }

    book(timetable_id, customer_id) {
        return this.http.post(ADMIN_API_HOST + '/book', {'customer_id' : customer_id, 'timetable_id': timetable_id})
            .map(res => res.json())
            .catch(this.handleError);
    }

    inqueue(timetable_id, customer_id) {
        return this.http.post(ADMIN_API_HOST + '/inqueue?', {customer_id : customer_id, timetable_id : timetable_id})
            .map(res => res.json())
            .catch(this.handleError);
    }

    cancel(timetable_id, customer_id) {
        return this.http.post(ADMIN_API_HOST + '/cancel', {customer_id : customer_id, timetable_id : timetable_id})
            .map(res => res.json())
            .catch(this.handleError);
    }

    forceCancel(timetable_id, customer_id) {
        return this.http.post(ADMIN_API_HOST + '/forceCancel', {customer_id : customer_id, timetable_id : timetable_id})
            .map(res => res.json())
            .catch(this.handleError);
    }

    login(account, password) {
        return this.http.post(ADMIN_API_HOST + '/login', {account:account, password:password})
            .map(res => res.json())
            .catch(this.handleError);
    }

    register(account, password, code) {
        return this.http.post(ADMIN_API_HOST + '/register', {account:account, password:password, code: code})
            .map(res => res.json())
            .catch(this.handleError);
    }

    getCustomer(customer_id) {
        let url = ADMIN_API_HOST + '/customer?1=1&customer_id=' + customer_id;
        let cacheKey = url;
        let request = this.http.get(url)
            .map(res => res.json())
            .catch(this.handleError);
        return this.cache.loadFromObservable(cacheKey, request);
    }

    feedback(customer_id, content) {
        return this.http.post(ADMIN_API_HOST + '/feedback', {customer_id: customer_id, content: content})
            .map(res => res.json())
            .catch(this.handleError);
    }

    getOrders(customer) {
        return this.http.get(order_URL + '?1=1&customer=' + customer)
            .map(res => res.json())
            .catch(this.handleError);
    }

    putOrders(id, data) {
        //let headers = new Headers();
        //headers.append('Content-Type', 'application/json');

        return this.http.put(order_URL + '/' + id, data)
            .map(res => res.json())
            .catch(this.handleError);
    }

    statistics(customer_id, device_token) {
        return this.http.post(ADMIN_API_HOST + '/statistics', {customer_id : customer_id, device_token : device_token})
            .map(res => res.json())
            .catch(this.handleError);
    }

    getSysConfig(key) {
        return this.http.get(ADMIN_API_HOST + '/sysconfig?key=' + key)
            .map(res => res.json())
            .catch(this.handleError);
    }

    handleError(error) {
        console.error(error);
        return Observable.throw(error.json().error || 'Server error');
    }

}
